#!/bin/bash

MIRROR_ALI=registry.cn-hangzhou.aliyuncs.com

create()
{
    SERVICE_NAME=$1
    SERVICE_VERSION=$2
    CONFIGS=${@:3}

    SERVICE=$SERVICE_NAME:$SERVICE_VERSION

    echo "deploy service:$SERVICE"

    docker service rm $SERVICE_NAME
    docker service create \
        --name $SERVICE_NAME \
        --with-registry-auth \
        $CONFIGS \
        $MIRROR_ALI/westeros/$SERVICE
}

update()
{
    SERVICE_NAME=$1
    SERVICE_VERSION=$2

    SERVICE=$SERVICE_NAME:$SERVICE_VERSION

    echo "deploy service:$SERVICE"

    docker service update \
        --force \
        --with-registry-auth \
        --image $MIRROR_ALI/westeros/$SERVICE \
        $SERVICE_NAME
}

case $1 in

"create")
    create ${@:2}
;;

"update")
    update ${@:2}
;;

*) echo "only accept params create|update" ;;
esac
